package com.lagou.rabbitmq.controller;

import com.lagou.rabbitmq.respository.PayRespository;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.UUID;

@Controller
public class PayController {
    @Autowired
    private AmqpTemplate rabbitTemplate;

    @Autowired
    PayRespository payRespository;

    @GetMapping("/pay")
    public String paySubmit(@RequestParam("money")String money,@RequestParam("bizId")String bizId,Map<String,Object> map) {
        if (payRespository.validBiz(bizId)){
            payRespository.set(bizId,money);
            map.put("list",payRespository.list());
            return "success.html";
        }else {
            map.put("list",payRespository.listTimeoutBiz());
            return "cancel.html";
        }

    }

    @GetMapping(value = "/topay")
    public String pay(Map<String,String> map) {
        String value = UUID.randomUUID().toString();
        map.put("bizId", value);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = simpleDateFormat.format(new Date());
        System.out.println("发送消息，time="+time+"，bizId="+value);
        rabbitTemplate.convertAndSend("ex.pay", "pay", value);
        return "topay.html";
    }

    @GetMapping(value = "/index")
    public String topay() {
        return "index.html";
    }
}